package com.rrd.hive.udf;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.CastDateToBooleanViaLongToLong;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author songxin
 */
public class UserGroupUDF extends UDF{
    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<>();
        arr.add("2020-02-25 15:12:48,5,,1,");
        arr.add(",2,,,");
        evaluate(arr);
    }
    public static Text evaluate(ArrayList<String> userLoanInfo) {
        boolean hasLoanSuccess = false;
        boolean isRepaying = false;
        String firstLoanTime = "";
        String lastLoanTime = "";
        String lastRepayTime = "";
        String lastQuotaChangeTime = "";
        String lastQuotaChangeAction = "";

        String[] LOAN_SUCCESS_STATUS = {"5", "7", "8"};
        String[] QUOTA_INCREASE = {"0004", "0006", "0007", "0008"};
        String[] QUOTA_DECREASE = {"0005", "0009"};

        String DEFAULT_USER_GROUP = "NEW";
        String userGroup = DEFAULT_USER_GROUP;
        String EMPTY_STRING = "";
        String REPAYING_STATUS = "2";
        String CAN_REPAY_STATUS = "1";
        if (userLoanInfo.toArray().length != 0) {
            for (String singleLoanInfo : userLoanInfo) {
                String[] loanInfo = singleLoanInfo.split(",", -1);
                System.out.println(loanInfo.length);
                String emitTime = loanInfo[0];
                String emitStatus = loanInfo[1];
                String repayTime = loanInfo[2];
                String repayStatus = loanInfo[3];
                String quotaChangeTime = loanInfo[4];
                String quotaChangeAction = loanInfo[5];
                String currentDate = loanInfo[6];

                if (!EMPTY_STRING.equals(emitTime) && Arrays.asList(LOAN_SUCCESS_STATUS).contains(emitStatus) && emitTime.compareTo(currentDate) < 0) {
                    // 本条放款信息为放款成功,并且放款时间小于计算日期，标记为曾放款成功，算作老客
//                    hasLoanSuccess = true;
//                    lastRepayTime = repayTime;
//                    lastLoanTime = emitTime;
//                    if (EMPTY_STRING.equals(firstLoanTime)) {
//                        // 记录首次放款成功的时间
//                        firstLoanTime = emitTime;
//                    }
                    userGroup = "OLD";
                    break;
                }
//                if (REPAYING_STATUS.equals(repayStatus) || CAN_REPAY_STATUS.equals(repayStatus)) {
//                    // 如果有还款中或者可还款的放款记录，则该用户为还款中
//                    isRepaying = true;
//                }
//                if (!EMPTY_STRING.equals(quotaChangeTime)) {
//                    // 如果有调额，每条数据都join到了最新的一条调额数据
//                    lastQuotaChangeTime = quotaChangeTime;
//                    lastQuotaChangeAction = quotaChangeAction;
//                }
            }
        }
//        if (!hasLoanSuccess) {
//            if (!EMPTY_STRING.equals(lastQuotaChangeTime)) {
//                if (Arrays.asList(QUOTA_INCREASE).contains(lastQuotaChangeAction)) {
//                    userGroup = "NEW_QUOTA_INCREASE";
//                }
//                else if (Arrays.asList(QUOTA_DECREASE).contains(lastQuotaChangeAction)) {
//                    userGroup = "NEW_QUOTA_DECREASE";
//                }
//            }
//        } else {
//            if (EMPTY_STRING.equals(lastRepayTime)) {
//                // 从未有过款款记录， 为首贷-余额再提现
//                userGroup = "OLD_BALANCE";
//            } else if (isRepaying) {
//                // 有过还款记录，并且有正在还款中的记录, 为老客-还款
//                userGroup = "OLD_REPAY";
//            } else {
//                // 有过还款记录，并且没有还款中和可还款的记录，为结清
//                userGroup = "OLD_PAYOFF";
//            }
//            if (lastQuotaChangeTime.compareTo(lastLoanTime) >= 0 && lastQuotaChangeTime.compareTo(lastRepayTime) >= 0) {
//                if (Arrays.asList(QUOTA_INCREASE).contains(lastQuotaChangeAction)) {
//                    userGroup = "OLD_QUOTA_INCREASE";
//                }
//                else if (Arrays.asList(QUOTA_DECREASE).contains(lastQuotaChangeAction)) {
//                    userGroup = "OLD_QUOTA_DECREASE";
//                }
//            }
//        }
        Text text = new Text();
        text.set(userGroup);
        System.out.println(text.toString());
        return text;
    }
}
